#ifndef ACCEL_H
#define ACCEL_H

#include <avr/eeprom.h>

#include <Vector3.h>

class Accelerometer;

struct accel_setting_t
{
		uint8_t mark;
		int16_t zero[3];
		
		void init()
		{
			  mark = 'A';
			  zero[0] = zero[1] = 0;
			  zero[2] = 512;
		} 
};

class Accelerometer
{
public:
    void init();
    void reset();
    void update();
    void calibrate();
    
    inline bool ready()
    {
        return calibrating == 0;
    }
    
    inline void save_setting();
    void load_setting();

    inline Vector3f& get_vector()
    {
        return vector;
    }
public:
    //int16_t raw_data[3];
    int16_t data[3];

    static const int16_t G;

private:
    inline void read(int16_t out[]);

private:
	
    accel_setting_t setting;
    
    int16_t calibrating;

    Vector3f vector;
    
    union
    {
        int32_t sum_32[3];
        int16_t sum_16[3];
    } sum;
};

#endif